﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KPIS.GERP.Models;
using KPIS.GERP.Models.FIM;
using KPIS.GERP.Models.SYS;
using WebMatrix.WebData;

namespace KPIS.GERP.WEB.GFMS.Controllers
{
    public class MasBankController : Controller
    {
        #region MasBank
        public ActionResult Index()
        {
            var service = new MasBankService.MasBankServiceClient();

            return View(service.SelectBank(new Models.FIM.MasBank { RecordStatus = "!D" },null));
            
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(MasBank model)
        {
            try
            {
                var service = new MasBankService.MasBankServiceClient();
                var count = service.SelectBank(new MasBank() { Code = model.Code, RecordStatus = "!D" }, null).Length;

                if (count > 0)
                {
                        ModelState.AddModelError("", "!เกิดข้อผิดพลาด รหัสธนาคารซ้ำ");
                        return View(model);
                }
                else
                {
                    if (ModelState.IsValid)
                    {
                        var id = service.InsertBank(model, WebSecurity.CurrentUserId);
                        return RedirectToAction("Details", new { id = id });

                    }
                }
            }
            catch (Exception ex)
            {
               
            }
            return View(model);
        }
        public ActionResult Edit(int id)
        {
            var service = new MasBankService.MasBankServiceClient();
            var model = service.SelectBank(new Models.FIM.MasBank { BankId = id },null).FirstOrDefault();

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(int id, MasBank model)
        {
            var service = new MasBankService.MasBankServiceClient();
            var oldModel = service.SelectBank(new MasBank() { BankId = model.BankId, RecordStatus = "!D" }, null).FirstOrDefault();
            var oldCode = oldModel.Code;
            var oldBankId = oldModel.BankId;

            try
            {

                if (oldCode == model.Code)
                {
                    if (ModelState.IsValid)
                    {
                        service.UpdateBank(model, WebSecurity.CurrentUserId);

                        return RedirectToAction("Details", new { id = id });
                    }
                }
                else
                {
                    var BankModels = service.SelectBank(new MasBank() { RecordStatus = "!D" }, null);
                    for (int i = 0; i < BankModels.Length; i++)
                    {
                        if (model.Code == BankModels[i].Code)
                        {
                            return Json(new { result = false, message = "!เกิดข้อผิดพลาด รหัสธนาคารซ้ำ", alertClass = "" });
                        }
                    }
                    service.UpdateBank(model, WebSecurity.CurrentUserId);
                    return RedirectToAction("Details", new { id = id });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return View(model);
        }

        public ActionResult Delete(int id)
        {
            try
            {
                var service = new MasBankService.MasBankServiceClient();
                service.DeleteBank(id, WebSecurity.CurrentUserId);

                return RedirectToAction("Index");
            }
            catch
            {
                return View("Index");
            }
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult DeleteAll(int[] deleteIds)
        {
            try
            {
                if (deleteIds != null)
                {
                    var service = new MasBankService.MasBankServiceClient();

                    foreach (int delete in deleteIds)
                    {
                        service.DeleteBank(delete, WebSecurity.CurrentUserId);
                    }
                }

                ModelState.AddModelError("Error", "ลบข้อมูลไปทั้งหมด " + deleteIds.Length.ToString("#,##0") + " รายการ");

                return Json(new { success = true });
            }
            catch
            {
                ModelState.AddModelError("Error", "พบข้อผิดพลาดขณะลบข้อมูล");
                return Json(new { success = false });
            }
        }

        #endregion

        #region MasBank

        public ActionResult Details(int id)
        {
            var service = new MasBankService.MasBankServiceClient();
            var model = service.SelectBank(new Models.FIM.MasBank { BankId = id }, null).FirstOrDefault();

            return View(model);
        }

        public ActionResult CreateBankBranch(int BankId)
        {
            var service = new MasBankService.MasBankServiceClient();
            var modelBankBranch = service.SelectBank(new Models.FIM.MasBank { BankId = BankId }, null).FirstOrDefault();
            var model = new MasBankBranch();
            model.BankId = modelBankBranch.BankId;

            return PartialView(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateBankBranch(MasBankBranch model)
        {
            try
            {
                var service = new MasBankService.MasBankServiceClient();
                var countBranch = service.SelectBankBranch(new MasBankBranch() { Code = model.Code, RecordStatus = "!D" }, null).Length;
                if (countBranch > 0)
                {
                    ModelState.AddModelError("", "!เกิดข้อผิดพลาด รหัสธนาคารซ้ำ");
                }
                else
                {
                    if (ModelState.IsValid)
                    {
                        var id = service.InsertBankBranch(model, WebSecurity.CurrentUserId);

                        return Json(new { result = true, message = "บันทึกเรียบร้อยแล้ว", alertClass = "" });
                    }
                }

            }
            catch (Exception ex)
            {
                return Json(new { result = false, message = ex.Message, alertClass = "" });
            }

            return Json(new { result = false, message = "!เกิดข้อผิดพลาด รหัสธนาคารซ้ำ", alertClass = "" });
        }

        public ActionResult EditBankBranch(int id)
        {
            var service = new MasBankService.MasBankServiceClient();
            var model = service.SelectBankBranch(new Models.FIM.MasBankBranch { BranchId = id }, null).FirstOrDefault();

            return PartialView(model);
        }

        [HttpPost]
        public ActionResult EditBankBranch(int id, MasBankBranch model)
        {
            var service = new MasBankService.MasBankServiceClient();
            var oldModel = service.SelectBankBranch(new MasBankBranch() { BranchId = model.BranchId }, null).FirstOrDefault();
            var oldCode= oldModel.Code;
            var oldBankId = oldModel.BankId;
            var oldBranchId = oldModel.BranchId;
            
            try
            {
                if (oldCode == model.Code)
                {
                    if (ModelState.IsValid)
                    {
                        service.UpdateBankBranch(model, WebSecurity.CurrentUserId);

                        return Json(new { result = true, message = "บันทึกเรียบร้อยแล้ว", alertClass = "" });
                    }
                }
                else
                {
                    var BranchModels = service.SelectBankBranch(new MasBankBranch() { BankId = model.BankId, RecordStatus = "!D" }, null);

                    for (int i = 0; i < BranchModels.Length; i++ )
                    {
                        if (model.Code == BranchModels[i].Code)
                        {
                            return Json(new { result = false, message = "!เกิดข้อผิดพลาด รหัสธนาคารซ้ำ", alertClass = "" });
                        }
                    }

                    service.UpdateBankBranch(model, WebSecurity.CurrentUserId);

                    return Json(new { result = true, message = "บันทึกเรียบร้อยแล้ว", alertClass = "" });
                }
            }
            catch (Exception ex)
            {
                return Json(new { result = false, message = ex.Message, alertClass = "" });
            }

            return Json(new { result = false, message = "กรอกข้อมูลไม่ครบ", alertClass = "" });
        }

        public ActionResult DeleteBankBranch(int id)
        {
            try
            {
                var service = new MasBankService.MasBankServiceClient();
                var BranchId = service.SelectBankBranch(new Models.FIM.MasBankBranch { BranchId = id }, null).FirstOrDefault().BankId;
                service.DeleteBankBranch(id, WebSecurity.CurrentUserId);

                return RedirectToAction("Details", new { id = BranchId });
            }
            catch
            {
                var service = new MasBankService.MasBankServiceClient();
                return View("Details", new { id = service.SelectBankBranch(new Models.FIM.MasBankBranch { BranchId = id }, null).FirstOrDefault().BankId });
            }
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult DeleteAllBankBranch(int[] deleteIds)
        {
            try
            {
                if (deleteIds != null)
                {
                    var service = new MasBankService.MasBankServiceClient();

                    foreach (int delete in deleteIds)
                    {
                        service.DeleteBankBranch(delete, WebSecurity.CurrentUserId);
                    }
                }

                ModelState.AddModelError("Error", "ลบข้อมูลไปทั้งหมด " + deleteIds.Length.ToString("#,##0") + " รายการ");

                return Json(new { success = true });
            }
            catch
            {
                ModelState.AddModelError("Error", "พบข้อผิดพลาดขณะลบข้อมูล");
                return Json(new { success = false });
            }
        }

        #endregion

    }
}
